<template>
    <orh-table reload  @on-reload="reload" ref="htmlHeight">
        
        <Button type="primary" size="small" @click="submits" slot="returntosuperior">升学年</Button>

        <Table border stripe :height="height" :loading="tableload" slot="table" :columns="columns" 
        v-for="(item, index) in data" :key="index" :style="index != 0?'margin-top: 10px;':''" :data="item.grades"></Table>

        <Modal v-model="entryModal" title="请选择届" @on-ok="dataSubmit" slot="others">
            <div>
                <RadioGroup v-model="cid">
                    <Radio v-for="(item, index) in cdata" :key="index" :label="item.id">{{ item.name.zh?item.name.zh:item.name.mn }}</Radio>
                </RadioGroup>
            </div>
        </Modal>
    </orh-table>
</template>
                
<script>

export default {
    components: {
    },
    data () {
        return {
            tableload: true,
            entryModal: false,
            data: [],
            height: 0,
            columns: [
                {
                    title: '年级',
                    align: 'center',
                    minWidth: 100,
                    render: (h, params) => {
                        let name = params.row.grade_name.zh?params.row.grade_name.zh:params.row.grade_name.mn
                        return h('div', name)
                    }
                },
                {
                    title: '当前届',
                    align: 'center',
                    minWidth: 100,
                    render: (h, params) => {
                        let name = params.row.arrive?params.row.arrive.name.zh?params.row.arrive.name.zh:params.row.arrive.name.mn:''
                        return h('div', name)
                    }
                },
                {
                    title: '下一届',
                    align: 'center',
                    minWidth: 100,
                    render: (h, params) => {
                        let name = params.row.next_arrive
                        let arr = []
                        if (name != null) {
                            return h('div', params.row.next_arrive.name)
                        } else {
                            params.row.newdata.forEach(el => {
                                arr.push(
                                    h('Option', {
                                        props: {
                                            value: el.id,
                                        }
                                    }, el.name.zh)
                                )
                            });
                            return h('Select', {
                                props: {
                                    size: 'small',
                                },
                                on: {
                                    'on-change': (event)=> {
                                        this.dataSubmit(params.row, event)
                                    }
                                }
                            }, arr)
                        }
                    }
                },
            ],
            url: 'api/eclass/v1/rise/list',
            curl: 'api/base/v1/get/not/study/arrives?period_id=',
            cdata: [],
            cid: null,
            condition: null,
            form: {
                rise_data: []
            },
            furl: 'api/base/v1/rise/school/year',
        }
    },
    methods: {

        // ==================================================
        // 提交数据
        // ==================================================
        dataSubmit(data, value) {
            let a = this.form.rise_data.find(i => i.period_id == data.period_id)
            if (a == undefined) {
                this.form.rise_data.push({
                    period_id: data.period_id,
                    arrive_id: value
                })
            } else {
                a.period_id = data.period_id,
                a.arrive_id = value
            }
        },

        // ==================================================
        // 确定提交
        // ==================================================
        submits(){
            if (this.form.rise_data.length != this.data.length) {
                this.$Message.warning('请录入全部信息');
                return
            }
            
            this.$Modal.confirm({
                title: '是否确认',
                content: '<p>学生升学年？</p>',
                loading: true,
                onOk: () => {
                    this.$http.post(this.curl).then(val=>{
                        this.$Modal.remove();
                    }).catch(err => {
                    })
                    this.$http.post(this.furl, this.form).then(val=>{
                        this.$Modal.remove();
                    }).catch(err => {
                    })
                }
            });
        },

        // ==================================================
        // 选择录入
        // ==================================================
        choiceData(data){
            this.$http.get(this.curl + data.id).then(val=>{
                let arr = val.data
                data.grades.forEach(i => {
                    i.newdata = arr;
                    this.cdata = arr;
                })
            }).catch(err => {
            })
        },

        // ==================================================
        // 手动刷新
        // ==================================================
        reload(){
            this.data = []
            this.form.rise_data = []
            this.tableload = true
            this.queryData()
        },

        // ==================================================
        // 列表高度
        // ==================================================
        styleData (){
            let a = this.$refs.htmlHeight.$el.offsetHeight
            this.height = (a - 135) / 2
        },

        // ==================================================
        // 请求数据
        // ==================================================
        queryData(){
            this.$http.get(this.url).then(val=>{
                this.styleData()
                let arr = []
                val.data.forEach(el => {
                    el.grades.forEach(i => {
                        i.newdata = []
                    })
                    this.choiceData(el)
                    arr.push(el)
                });
                this.data = arr
                this.tableload = false
            }).catch(err => {
                this.tableload = false
            })
        },
    },
    created(){
        this.queryData()
    },
};
</script>

